<?php
namespace Home\Controller;
use Common\Controller\AuthController;
class ArticleController extends AuthController {
    public function index(){//显示文章
		$Article = D("Article");
		$count = $Article->count();
		$Page = new \Think\Page($count,10);
		$list = $Article->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->relation(true)->select();
		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('list',$list);//一个参数‘’表示在模版取值用的变量名，第二个参数是  变量的值
		$this->display();
	}
	public function add(){//添加文章
		$Fl = M("Fl");
		$list = $Fl->order('flpx asc')->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function tjadd(){//提交添加文章
		if(IS_POST){
			$Article = D("Article");
			if(!$Article->create($_POST,4)){
				$error = $Article->getError();
				$this->error($error,U('Article/add'),1);
			}else{
				$map['arname'] = $_POST['arname'];
				$map['arcontent'] = $_POST['arcontent'];
				$map['flid'] = $_POST['flid'];
				$map['ardate'] = date('Y-m-d H:i:s');
				$result = $Article->add($map);
				if($result){
					$this->success("文章添加成功",U('Article/index'));
				}else{
					$this->error("文章添加失败",U('Article/add'));
				}
			}
		}else{
			$this->error("添加失败",U('Article/add'));
		}
	}
	public function arbj($id){//显示修改文章
		$Article = D("Article");
		$map['id'] = $id;
		$result = $Article->where($map)->relation(true)->find();
		$Fl = D("Fl");
		$fenlei = $Fl->order('flpx asc')->select();
		$this->assign('result',$result);
		$this->assign('fenlei',$fenlei);
		$this->display();
	}
	public function arbjtj(){//提交修改文章
		if(IS_POST){
			$Article = D("Article");
			if(!$Article->create($_POST,4)){
				$error = $Article->getError();
				$this->error($error,U('Article/arbj',array('id'=>'$_POST[id]')),1);
			}else{
				$map['arname'] = $_POST['arname'];
				$map['arcontent'] = $_POST['arcontent'];
				$map['flid'] = $_POST['flid'];
				$map['ardate'] = date('Y-m-d H:i:s');
				$where['id'] = $_POST['id'];
				$result = $Article->where($where)->save($map);
				if($result){
					$this->success("文章修改成功",U('Article/index'));
				}else{
					$this->error("文章修改失败",U('Article/add'));
				}
			}
		}else{
			$this->error("修改失败",U('Article/arbj',array('id'=>'$_POST[id]')),1);
		}
	}
	
	public function delete($id){//删除文章
		$Article = M("Article");
		$where['id'] = $id;
		$result = $Article->where($where)->delete();
		if($result){
			$this->success("删除成功！",U('Article/index'));
		}else{
			$this->error("删除失败！",U('Article/index'));
		}
	}
}